package gonz.integration;

import gonz.VS;
import gonz.common.Config;
import gonz.common.Waiter;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.VkontakteApi;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;

public class VkPublisher {
    public static final String SECRET_KEY = Config.settings.getString(Config.VK_SECRET_KEY_KEY);
    public static final String FOOTER_ELEMENT_ID = "footer_wrap";
    public static final String EMAIL_INPUT_XPATH="//input[@name='email']";
    public static final String PASS_INPUT_XPATH="//input[@name='pass']";
    public static final String SUBMIT_ID="install_allow";
    public static final String ALLOW_BUTTON_ID = "install_allow";
    public static final String API_URL = "https://api.vk.com/method/";

    public static final Logger LOGGER = Logger.getLogger(VkPublisher.class);

    public void publish() {
        LOGGER.debug("Publishing image to VK.");
        //Step One: Create the OAuthService object
        OAuthService service = new ServiceBuilder()
                .provider(VkontakteApi.class)
                .apiKey(Config.settings.getString(Config.VK_APP_ID_KEY))
                .apiSecret(Config.settings.getString(Config.VK_SECRET_KEY_KEY))
                .scope("groups")
                .callback("http://oauth.vk.com/blank.html")
                .build();
        //Step Two: Get the request token
        //Token requestToken = service.getRequestToken();
        //Step Three: Making the user validate your request token
        String authUrl = service.getAuthorizationUrl(null);
        LOGGER.debug("Authorization Url is: " + authUrl);
        WebDriver webDriver = VS.setUpWebDriver();
        webDriver.get(authUrl);
        Waiter.waitElement(webDriver, By.id(FOOTER_ELEMENT_ID));
        WebElement emailElement = webDriver.findElement(By.xpath(EMAIL_INPUT_XPATH));
        emailElement.sendKeys(Config.settings.getString(Config.VK_LOGIN_KEY));
        WebElement passElement = webDriver.findElement(By.xpath(PASS_INPUT_XPATH));
        passElement.sendKeys(Config.settings.getString(Config.VK_PASS_KEY));
        WebElement submit = webDriver.findElement(By.id(SUBMIT_ID));
        submit.click();
        WebElement allow = webDriver.findElement(By.id(ALLOW_BUTTON_ID));
        allow.click();
        String url = webDriver.getCurrentUrl();
        String code = StringUtils.substringAfter(url, "code=");
        if (StringUtils.isBlank(code)) {
            LOGGER.error("Can't receive autn code. " + url);
        } else {
            LOGGER.debug("Auth code is: " + code);
            return;
        }
        VS.tearDownWebDriver(webDriver);
        //Step Four: Get the access Token
        Verifier verifier = new Verifier(code);
        Token accessToken = service.getAccessToken(null, verifier);
        LOGGER.debug("Got the Access Token: " + accessToken);
        //Step Five: Sign request
        OAuthRequest request = new OAuthRequest(Verb.POST, "http://api.twitter.com/1/account/verify_credentials.xml");
        service.signRequest(accessToken, request); // the access token from step 4
        request.addHeader("Content-Type", "multipart/form-data; boundary=");
//        request.addPayload();
//        Response response = request.send();
//        System.out.println(response.getBody());




    }
    private String getMethodUrl(String method) {
        return API_URL + method + ".xml";
    }
}
